package com.yiyuan.km.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.ServletActionContext;
import org.json.JSONObject;

import com.yiyuan.common.base.BaseAction;
import com.yiyuan.km.model.Knowledge;
import com.yiyuan.km.service.IKmService;

public class KmAction extends BaseAction {

	private static final long serialVersionUID = 1L;
	/* ************* 分页参数封装**************************************** */
	private String startrecord;
	private String endrecord;
	private String perpage;
	/* ************* 参数封装******************************************* */
	private String id;
	private String title;
	private String typeId;
	private String content;

	/* ************* 输出结果封装***************************************** */
	private Knowledge knowledge;
	private List<Knowledge> kmList;

	// 资源引用
	@Resource
	private IKmService kmService;

	/** *****************知识操作******************************* */
	//进入知识管理主页
	public String index(){
		return "toMain";
	}
	//
	@SuppressWarnings("unchecked")
	// 获得知识列表
	public void listKm() {
		int start = 1;
		int end = 100;

		// 三个内置分页参数
		if (this.startrecord != null && this.endrecord != null
				&& this.perpage != null) {
			start = Integer.parseInt(this.startrecord);
			end = Integer.parseInt(this.endrecord);
		}
		// 获得页数及每页记录数
		int totalRecord = kmService.getKmPageCount();
		kmList = kmService.findKmList(start, end);

		// 封装输出结果
		Map map = new HashMap();
		map.put("list", kmList);
		map.put("totalRecord", totalRecord);
		JSONObject jso = new JSONObject(map);

		try {
			ServletActionContext.getResponse().setCharacterEncoding("UTF-8");
			ServletActionContext.getResponse().setContentType(
					"text/xml; charset=utf-8");

			ServletActionContext.getResponse().getWriter()
					.print(jso.toString());
		} catch (Exception e) {

		}
	}

	// 保存知识
	public void saveKm() {

	}

	// 获得文件详细
	public void loadKm() {

	}

	// 删除文件
	public void delKm() {

	}

	//进入发布知识页面
	public String toAdd(){
		return "toAdd";
	}
	
	/** ******************************************************* */
	public String getStartrecord() {
		return startrecord;
	}

	public void setStartrecord(String startrecord) {
		this.startrecord = startrecord;
	}

	public String getEndrecord() {
		return endrecord;
	}

	public void setEndrecord(String endrecord) {
		this.endrecord = endrecord;
	}

	public String getPerpage() {
		return perpage;
	}

	public void setPerpage(String perpage) {
		this.perpage = perpage;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getTypeId() {
		return typeId;
	}

	public void setTypeId(String typeId) {
		this.typeId = typeId;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public Knowledge getKnowledge() {
		return knowledge;
	}

	public void setKnowledge(Knowledge knowledge) {
		this.knowledge = knowledge;
	}

	public List<Knowledge> getKmList() {
		return kmList;
	}

	public void setKmList(List<Knowledge> kmList) {
		this.kmList = kmList;
	}
}
